/**
 * Copyright (c) 2018-2099, Chill Zhuang 庄骞 (bladejava@qq.com).
 * <p>
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/lgpl.html
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springblade.modules.restaurant.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.restaurant.constant.RestaurantConstant;
import org.springblade.modules.restaurant.entity.UserSalary;
import org.springblade.modules.restaurant.service.IUserSalaryService;
import org.springblade.modules.restaurant.vo.UserSalaryVO;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * 用户薪资配置表 控制器
 *
 * @author Blade
 * @since 2025-02-18
 */
@RestController
@AllArgsConstructor
@RequestMapping("/restaurant/usersalary")
@Tag(name = "用户薪资配置表", description = "用户薪资配置表接口")
public class UserSalaryController extends BladeController {

	private IUserSalaryService userSalaryService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@Operation(summary = "详情", description = "传入userSalary")
	public R<UserSalary> detail(UserSalary userSalary) {
		UserSalary detail = userSalaryService.getOne(Condition.getQueryWrapper(userSalary));
		return R.data(detail);
	}

	/**
	 * 分页 用户薪资配置表
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@Operation(summary = "分页", description = "传入userSalary")
	public R<IPage<UserSalary>> list(UserSalary userSalary, Query query) {
		IPage<UserSalary> pages = userSalaryService.page(Condition.getPage(query), Condition.getQueryWrapper(userSalary));
		return R.data(pages);
	}

	/**
	 * 自定义分页 用户薪资配置表
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@Operation(summary = "分页", description = "传入userSalary")
	public R<IPage<UserSalaryVO>> page(UserSalaryVO userSalary, Query query) {
		IPage<UserSalaryVO> pages = userSalaryService.selectUserSalaryPage(Condition.getPage(query), userSalary);
		return R.data(pages);
	}

	/**
	 * 新增 用户薪资配置表
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@Operation(summary = "新增", description = "传入userSalary")
	public R save(@Valid @RequestBody UserSalary userSalary) {
		// 判断是否已经有数据
		UserSalary one = userSalaryService.getOne(Wrappers.<UserSalary>lambdaQuery()
			.eq(UserSalary::getUserId, userSalary.getUserId()));
		if (one != null) {
			return R.fail(one.getUserName() + "数据已存在");
		}

		// 更新日薪
		BigDecimal dailySalary = userSalary.getMonthlySalary().divide(BigDecimal.valueOf(RestaurantConstant.MONTH_DAYS), 2, RoundingMode.HALF_UP);
		userSalary.setDailySalary(dailySalary);

		return R.status(userSalaryService.customizeSave(userSalary));
	}

	/**
	 * 修改 用户薪资配置表
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@Operation(summary = "修改", description = "传入userSalary")
	public R update(@Valid @RequestBody UserSalary userSalary) {

		BigDecimal dailySalary = userSalary.getMonthlySalary().divide(BigDecimal.valueOf(RestaurantConstant.MONTH_DAYS), 2, RoundingMode.HALF_UP);
		userSalary.setDailySalary(dailySalary);

		return R.status(userSalaryService.customizeSave(userSalary));
	}

	/**
	 * 新增或修改 用户薪资配置表
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@Operation(summary = "新增或修改", description = "传入userSalary")
	public R submit(@Valid @RequestBody UserSalary userSalary) {

		// 更新日薪
		BigDecimal dailySalary = userSalary.getMonthlySalary().divide(BigDecimal.valueOf(RestaurantConstant.MONTH_DAYS), 2, RoundingMode.HALF_UP);
		userSalary.setDailySalary(dailySalary);

		return R.status(userSalaryService.customizeSave(userSalary));
	}


	/**
	 * 删除 用户薪资配置表
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@Operation(summary = "逻辑删除", description = "传入ids")
	public R remove(@Parameter(description = "主键集合", required = true) @RequestParam String ids) {
		return R.status(userSalaryService.deleteLogic(Func.toLongList(ids)));
	}

}
